package com.abcdefg.springdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DogController {
    @Autowired
    private Dog singleDog;
    @Autowired
    private Dog prototypeDog;
    @Autowired
    private Dog requestDog;
    @Autowired
    private Dog sessionDog;
    @Autowired
    private Dog applicationDog;
    @Autowired
    private ApplicationContext applicationContext;

    //单例模式
    @RequestMapping("/single")
    public String singleDog(){
        Dog contextDog=(Dog)applicationContext.getBean("singleDog");
        return "dog"+ singleDog.toString()+"contextDog"+contextDog;
    }
    //非单例模式
    @RequestMapping("/prototype")
    public String prototype(){
        Dog contextDog=(Dog)applicationContext.getBean("prototypeDog");
        return "dog"+ prototypeDog.toString()+"contextDog"+contextDog;
    }
    //每个HTTP请求生命周期，创建新实例
    @RequestMapping("/request")
    public String request(){
        Dog contextDog=(Dog)applicationContext.getBean("requestDog");
        return "dog"+ requestDog.toString()+"contextDog"+contextDog;
    }
    //每个session生命周期是同一个实例
    @RequestMapping("/session")
    public String session(){
        Dog contextDog=(Dog)applicationContext.getBean("sessionDog");
        return "dog"+ sessionDog.toString()+"contextDog"+contextDog;
    }
    //每个ServletContext生命周期创建实例
    @RequestMapping("/application")
    public String application(){
        Dog contextDog=(Dog)applicationContext.getBean("applicationDog");
        return "dog"+ applicationDog.toString()+"contextDog"+contextDog;
    }
}
